home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter10
/
TargetViewpoint1.java
< prev
next >
Wrap
Text File
|
2000-07-11
|
2KB
|
89 lines
package custom_nodes;
import shout3d.core.*;
import shout3d.math.*;
public class TargetViewpoint1 extends Viewpoint implements FieldObserver {
final public FloatField heading = new FloatField(this, "heading", Field.ANY, 0);
final public FloatField pitch = new FloatField(this, "pitch", Field.ANY, 0);
final public FloatField distance = new FloatField(this, "distance", Field.NON_NEGATIVE_FLOAT, 10);
Quaternion q = new Quaternion();
float[] eulers = {0.0f, 0.0f, 0.0f};
//default constructor
public TargetViewpoint1(){
this(null);
}
//constructor that takes
//a viewer argument
public TargetViewpoint1(Shout3DViewer viewer){
//call Viewpoint's constructor
super(viewer);
//register as FieldObserver
heading.addFieldObserver(this, null);
pitch.addFieldObserver(this, null);
distance.addFieldObserver(this, null);
//compute position and oreintation
setViewpoint();
}
protected void finalize() throws Throwable {
heading.removeFieldObserver(this);
pitch.removeFieldObserver(this);
distance.removeFieldObserver(this);
super.finalize();
}
public void onFieldChange(Field theField, Object userData) {
//if TargetViewpoint fields have changed
if ( theField == heading || theField == pitch || theField == distance) {
setViewpoint();
}
//if something else has changed
else {
// call Viewpoint's onFieldChange()
super.onFieldChange(theField, userData);
}
}
public void setViewpoint() {
//set quaternion to current heading and pitch
eulers[0] = heading.getValue();
eulers[1] = pitch.getValue();
q.setEulers(eulers);
//set distance vector
//and rotate it
if ( distance.getValue() < 0.0f) {
distance.setValue(0.0f);
}
float[] vector = {0.0f, 0.0f, distance.getValue()};
q.xform(vector);
//set Viewpoint rotation and
//translate to end of vector
float[] axisAngle = new float[4];
q.getAxisAngle(axisAngle);
orientation.setValue(axisAngle);
position.setValue(vector);
}
}